﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace DemoPersonalizeSearch
{
    class QueryExpansion
    {
        private List<List<string>> ReadData()
        {
            string path = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "App_Data");
            StreamReader sr = new StreamReader(path+"\\Result.eps");
            List<List<string>> list = new List<List<string>>();
            while (!sr.EndOfStream)
            {
                 
                string s = sr.ReadLine();
                string[] str = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                List<string> listLine = str.ToList();
                list.Add(listLine);
            }
            return list;
        }
        public List<string> ExpansionQuery(string query,int mode,UserData ud)
        {
            List<List<string>> list = ReadData();
            List<string> expansion = new List<string>();
            //case 1
            string[] str = query.Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            List<List<string>> listTemp = new List<List<string>>();
            foreach (var line in list)
            {

                if (mode == 1)
                {
                    bool isoke = true;
                    foreach (var s in str)
                    {
                        if (!line.Contains(s))
                        {
                            isoke = false;
                            break;
                        }

                    }

                    if (isoke)
                        expansion.AddRange(line);
                }
                else if (mode == 2)
                {
                    bool isoke = false;
                    foreach (var s in str)
                    {
                        if (line.Contains(s))
                        {
                            isoke = true;
                            break;
                        }
                    }
                    if (isoke)
                        expansion.AddRange(line);
                }
            }
            if (ud == null)
            {
                expansion = expansion.Where(x => !str.Contains(x)).GroupBy(x => x).Select(x => new { Key = x.Key, count = x.Count() }).OrderByDescending(x => x.count).Select(x => x.Key).Take(10).ToList();
            }
            else
            {
                double gamma = 0.5;
                var epx1 = expansion.Where(x => !str.Contains(x)).GroupBy(x => x).Select(x => new { Key = x.Key, count = x.Count() });
               int min = epx1.Min(x => x.count);
               int max = epx1.Max(x => x.count);
               //get user data
               UserItem ui = new UserItem();
               ui.ParseUserData(ud);
               double minu = ui.Tags.Min(x => x.Weight);
               double maxu = ui.Tags.Max(x => x.Weight);

               expansion = epx1.OrderByDescending(x => ((x.count-min*1.0)/(max-min))*gamma + ((ui.GetWeightByTagName(x.Key))/(maxu))*(1-gamma)).Select(x => x.Key).Take(10).ToList();
            }

      
            return expansion;

        }
        
    }
}
